package hong.jun;

import java.io.*;

/**
 * 序列化transient关键字
 */
public class TransientTest {
    public static void main(String[] args) throws Exception, IOException {
        SerializeUser();
        DeSerializeUser();
    }

    //序列化
    private static void SerializeUser() throws FileNotFoundException, IOException, ClassNotFoundException {
        User user = new User();
        user.setName("Java的架构师技术栈");
        user.setAge(24);
        ObjectOutputStream oos =
                new ObjectOutputStream(new FileOutputStream("template.txt"));
        oos.writeObject(user);
        oos.close();
        System.out.println("添加了transient关键字序列化：age=  "+user.getAge());
    }
    //反序列化
    private static void DeSerializeUser() throws IOException, ClassNotFoundException {
        File file = new File("template.txt");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        User newUser = (User)ois.readObject();
        //age添加了transient关键字，所以无法序列化
        System.out.println("添加了transient关键字反序列化：age=  "+newUser.getAge());
    }
}
